﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.UIElements;

[System.Serializable]
public class ApplyPanelData
{
    public List<Apply> applies;
}

public partial class ApplyPanel : AppUI
{
    List<ApplicationItem> m_ReceiveApplies = new List<ApplicationItem>();
    public Transform receiveProcessedApplyItemsParent;
    public Transform receiveNotProcessApplyItemsParent;
    public ApplicationItem receiveApplyItemPref;
    List<ApplicationItem> m_RequestApplies = new List<ApplicationItem>();
    public Transform requestApplyItemsParent;
    public ApplicationItem requestApplyItemPref;

    public GameObject receivedItemsParent;
    public GameObject requestItemsParent;

    ApplyPanelData m_Data;

    public static int lastSelectToggle = 1;

    public const int Toggle_Receive = 1;
    public const int Toggle_Request = 2;

    public ListTogglesGroup togglesGroup;

    public override void Start()
    {
        if (togglesGroup)
            togglesGroup.Init(OnToggleSelect, lastSelectToggle);

        receiveApplyItemPref.gameObject.SetActive(false);
        requestApplyItemPref.gameObject.SetActive(false);
    }

    public void Toggle(int id)
    {
        lastSelectToggle = id;

        if (togglesGroup)
            togglesGroup.Init(OnToggleSelect, lastSelectToggle);
    }

    void OnToggleSelect(int id)
    {
        lastSelectToggle = id;

        if (id == Toggle_Receive)
        {
            receivedItemsParent.gameObject.SetActive(true);
            requestItemsParent.gameObject.SetActive(false);
        }
        else if (id == Toggle_Request)
        {
            receivedItemsParent.gameObject.SetActive(false);
            requestItemsParent.gameObject.SetActive(true);
        }
    }

    public void Init(ApplyPanelData data)
    {
        m_Data = data;

        UnityFunctions.ClearList(m_ReceiveApplies);
        UnityFunctions.ClearList(m_RequestApplies);

        int processedCount = 0;
        int notProcessCount = 0;

        for (int i = 0; i < data.applies.Count; i++)
        {
            var apply = data.applies[i];
            if (apply.targetUserId == MGame.instance.MUser.id)
            {
                Transform parent = null;
                if(apply.result == Apply.Applying)
                {
                    notProcessCount++;
                    parent = receiveNotProcessApplyItemsParent;

                    //GameDebug.LogError($"{apply.sourceUserId} Applying ");
                }
                else
                {
                    processedCount++;
                    parent = receiveProcessedApplyItemsParent;

                    //GameDebug.LogError($"{apply.sourceUserId}  processed ");
                }
                var newItem = Instantiate(receiveApplyItemPref, parent);
                newItem.gameObject.SetActive(true);
                newItem.Init(apply, OnSelectApplicationItem);

                m_ReceiveApplies.Add(newItem);
            }
            else
            {
                var newItem = Instantiate(requestApplyItemPref, requestApplyItemsParent);
                newItem.gameObject.SetActive(true);
                newItem.Init(apply, OnSelectApplicationItem);

                m_RequestApplies.Add(newItem);
            }
        }

        StartCoroutine(UnityFunctions.AdjustItemsLayout(transform));
    }

    public void OnSelectApplicationItem(ApplicationItem item)
    {
        ApplyUserPanel view = this.GetComponentInParent<UIController>().ShowUI<ApplyUserPanel>();
        view.Init(this.m_Data.applies.Find(e => e.targetUserId == item.data.targetUserId));
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    //public override void GoBack()
    //{
    //    UIController.ShowUI<MainPanel>();
    //}


}
